package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.TradeDetailsPnlAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.PnlDetailsInfo;
import com.lanlian.commonlib.bean.ReqPnlDetails;
import com.lanlian.commonlib.bean.RespPnlDetails;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 16:51
 *     desc   : 盈亏明细
 *     version: 1.0
 * </pre>
 */
public class TradeDetailsPnlActivity extends BaseActivity {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;

    private ArrayList<PnlDetailsInfo> mList = new ArrayList<>();
    private TradeDetailsPnlAdapter mAdapter;
    private Long mDealNo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade_details_pnl, true);
        initData();
        initView();
        setListeners();
        initAdapter();
        mLayRefresh.autoRefresh();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("盈亏明细");
        mRecycleView = findViewById(R.id.recycle_view);
        mLayRefresh = findViewById(R.id.lay_refresh);
    }

    @Override
    protected void initData() {
        super.initData();
        mDealNo = getIntent().getLongExtra("dealNo", 0);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mRecycleView.scrollToPosition(0);
                getDealDetails();
            }
        });
    }

    private void initAdapter() {
        mAdapter = new TradeDetailsPnlAdapter(R.layout.adapter_trade_details_pnl, mList);
        mRecycleView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecycleView.setLayoutManager(linearLayoutManager);
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.lay_empty_details);
    }

    private void getDealDetails() {
        ReqPnlDetails reqPnlDetails = new ReqPnlDetails();
        reqPnlDetails.setlDealNo(mDealNo);
        VolleyRequestUtils.getInstance().getPnlDetails(this.toString(), reqPnlDetails,
                new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mLayRefresh.finishRefresh();
                        RespPnlDetails respPnlDetails = (RespPnlDetails) response;
                        mList = respPnlDetails.getDataList();
                        mAdapter.setNewData(mList);
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        mLayRefresh.finishRefresh();
                        showToast(baseResponse.getMsg());
                    }
                });
    }

}
